package com.bw.lib_log.logger

import android.util.Log
import com.bw.lib_log.LoggerLevel
import com.bw.lib_log.file.LogFileManager
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class FileLogger:ILogger {

    private var isDebug = true
    private var TAG = ""
    private var level = LoggerLevel.DEBUG
    private var loggerFormat = "===>$TAG %s ---> %s"//格式化 %s占位符
    private var path = ""

    private var fileManager:LogFileManager? = null
    override fun setDebug(isDebug: Boolean) {
        this.isDebug = isDebug
    }

    override fun setLevel(level: LoggerLevel) {
        this.level = level
    }

    override fun setTag(tag: String) {
        this.TAG = tag
    }

    override fun setSavaUrl(path: String) {
        this.path = path
        fileManager = LogFileManager(File(path))
    }

    private fun getCurrentTime():String{
        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault()).format(Date())
    }
    override fun v(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.VERBOSE.value) {
            val log = "写到磁盘中"+String.format(loggerFormat, tag, log)
            Log.v(TAG, log)
        }
    }

    override fun d(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.DEBUG.value) {
            val log = "写到磁盘中"+String.format(loggerFormat, tag, log)
            Log.d(TAG, log)
        }
    }

    override fun i(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.INFO.value) {
            val log = "写到磁盘中"+String.format(loggerFormat, tag, log)
            Log.i(TAG, log)
        }
    }

    override fun w(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.WARN.value) {
            val log = "写到磁盘中"+String.format(loggerFormat, tag, log)
            Log.w(TAG, log)
        }
    }

    override fun e(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.ERROR.value) {
            val log = "写到磁盘中"+String.format(loggerFormat, tag, log)
            Log.e(TAG, log)
        }
    }


}